export const Storage = {
  set: (key, value, expire = -1) => {
    const obj = {
      data: value,
      time: Date.now(),
      expire: expire
    }
    localStorage.setItem(key, JSON.stringify(obj))
  },
  get: key => {
    let val = localStorage.getItem(key)
    if (!val) {
      return null
    }
    val = JSON.parse(val)
    if (val.expire === -1) {
      return val.data
    } else if (Date.now() - val.time > val.expire * 1000) {
      localStorage.removeItem(key)
      return null
    } else {
      return val.data
    }
  },
  remove: key => localStorage.removeItem(key),
  clear: () => localStorage.clear()
}

// 日期格式化
export function formatOnlyDate () {
  const dateTime = new Date()
  const year = dateTime.getFullYear()
  let month = dateTime.getMonth() + 1
  month = month < 10 ? '0' + month : month
  let date = dateTime.getDate()
  date = date < 10 ? '0' + date : date
  return year + '-' + month + '-' + date
}

// 比较两个数组是否一致 不包含顺序
export function judgeArr (arr1, arr2) {
  let flag = true
  if (arr1.length !== arr2.length) {
    flag = false
  } else if (arr1.length === 0) {
    flag = true
  } else {
    arr1.some(item => {
      if (arr2.indexOf(item) === -1) {
        flag = false
        return true
      }
    })
  }
  return flag
}
